<template>
    <el-dialog v-dialogDrag title="备注" :visible="epcShow" width="85%" @close="dialogClose" :close-on-click-modal="false">
        <el-form ref="marketForm" :model="marketForm" :rules="rules" label-position="left" style="width: 100%;">
            <el-form-item label="VIN：" prop="vin">
                <el-input v-model="marketForm.vin" prefix-icon="el-icon-search" style="width:25%"
                    @keyup.enter.native="search()"></el-input>
                <el-button type="primary" @click="search()" class="margin-l-25">查询 </el-button>
            </el-form-item>
            <el-form-item label="名称：" prop="carName">
                <el-input type="text" v-model="marketForm.carName" readonly style="width:25%"></el-input>
            </el-form-item>
            <el-form-item label="车型：" prop="amVehicleId">
                <el-select v-model="marketForm.amVehicleId" clearable style="width:25%" @change="selectCar">
                    <el-option v-for="item in optionsCar" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <p style="text-align: right;font-size: 12px;color: #333;padding-right: 2px;">
                    单价：元/个</p>
                <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top: 1px;"
                    :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
                    <el-table-column label="零部件名称" prop="oeName"> </el-table-column>
                    <el-table-column label="原厂零件号" prop="oe"> </el-table-column>
                    <el-table-column label="厂商指导价" prop="guidePrice"> </el-table-column>
                    <el-table-column label="华东区市场价" prop="marketPriceHd"></el-table-column>
                    <el-table-column label="华南区市场价" prop="marketPriceHn"></el-table-column>
                    <el-table-column label="西部区市场价" prop="marketPriceXb"></el-table-column>
                    <el-table-column label="东北区市场价" prop="marketPriceDb"></el-table-column>
                    <el-table-column label="华北区市场价" prop="marketPriceHb"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" style="color:#f56c6c" @click="canle(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item label="市场区域价格：" prop="market">
                <el-select v-model="marketForm.market" clearable="">
                    <el-option label="厂商指导价" value="guidePrice"></el-option>
                    <el-option label="华东区市场价" value="marketPriceHd"></el-option>
                    <el-option label="华南区市场价" value="marketPriceHn"></el-option>
                    <el-option label="西部区市场价" value="marketPriceXb"></el-option>
                    <el-option label="东北区市场价" value="marketPriceDb"></el-option>
                    <el-option label="华北区市场价" value="marketPriceHb"></el-option>
                </el-select>
                <p style="text-align: right;"><el-button type="primary" @click="submit">保存</el-button></p>
            </el-form-item>
        </el-form>

    </el-dialog>
</template>
<script>
import { getPrice, findByvin } from '@/api/Epc/epc';
export default {
    name: 'Epc',
    props: {
        epcShow: {
            type: Boolean
        },
        // table: {
        //     type: Array
        // }
    },

    data () {
        return {
            tableData: [],
            optionsCar: [],
            marketForm: {},
            table:[],
            rules: {
                market: [{
                    required: true,
                    message: "请选择市场区域",
                    trigger: "change,blur",
                }]
            }

        }
    },
    methods: {
        search () {
            this.optionsCar = []
            findByvin(this.marketForm.vin).then(res => {
                this.marketForm.carName = res.data[0].amMainBrandName + '-' + res.data[0].amSeriesName
                res.data.forEach(item => {
                    this.optionsCar.push({
                        label: item.amVehicleName,
                        value: item.amVehicleId
                    })
                });
            })
        },
        selectCar () {
            getPrice({ userId: Number(localStorage.getItem('userId')), amVehicleId: this.marketForm.amVehicleId }).then((res) => {
                if (res.data) {
                    this.tableData = res.data
                }
            })

        },
        dialogClose () {
            this.$emit('dialogClose')
        },
        submit () {
            this.$refs.marketForm.validate((valid) => {
                if (valid) {
                    for (let i = 0; i < this.tableData.length; i++) {
                        let obj = {
                            lbjmc: this.tableData[i].oeName,
                            ycljh: this.tableData[i].oe,
                            price: this.tableData[i][this.marketForm.market],
                            count: '',
                            amount: '',
                            remark: ""
                        }
                        this.table.push(obj)
                    }
                    this.$emit('gettable')
                    this.$emit('dialogClose')
                } else {
                    return
                }
            })
        },
    },
    mounted () {
        // getPrice({ userId: Number(localStorage.getItem('userId')) }).then((res) => {
        //     this.tableData = res.data
        // })
    }
}
</script>